<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown预览工具</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.3.0/marked.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            animation: slideUp 0.8s ease-out;
        }
        
        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .header {
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            color: white;
            padding: 30px;
            text-align: center;
            position: relative;
            overflow: hidden;
        }
        
        .header::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { left: -100%; }
            100% { left: 100%; }
        }
        
        .header h1 {
            font-size: 2.5rem;
            font-weight: 700;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }
        
        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }
        
        .controls {
            padding: 30px;
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
            border-bottom: 1px solid #dee2e6;
            display: flex;
            align-items: center;
            gap: 15px;
            flex-wrap: wrap;
        }
        
        .file-input-wrapper {
            position: relative;
            display: inline-block;
        }
        
        .file-input {
            position: absolute;
            opacity: 0;
            width: 100%;
            height: 100%;
            cursor: pointer;
        }
        
        .file-button {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 12px 24px;
            background: linear-gradient(135deg, #4CAF50, #45a049);
            color: white;
            border-radius: 8px;
            font-weight: 600;
            text-decoration: none;
            transition: all 0.3s ease;
            cursor: pointer;
            border: none;
            font-size: 14px;
        }
        
        .file-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 16px rgba(76, 175, 80, 0.3);
        }
        
        .action-btn {
            padding: 12px 24px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        
        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3);
        }
        
        .action-btn.secondary {
            background: linear-gradient(135deg, #28a745, #20c997);
        }
        
        .action-btn.secondary:hover {
            box-shadow: 0 8px 16px rgba(40, 167, 69, 0.3);
        }
        
        .main-content {
            display: flex;
            height: 70vh;
            min-height: 600px;
        }
        
        .panel {
            flex: 1;
            display: flex;
            flex-direction: column;
        }
        
        .editor-panel {
            border-right: 1px solid #e0e0e0;
        }
        
        .panel-header {
            background: #f8f9fa;
            padding: 15px 20px;
            font-weight: 600;
            color: #2c3e50;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .editor {
            flex: 1;
            resize: none;
            border: none;
            padding: 20px;
            font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', monospace;
            font-size: 14px;
            line-height: 1.6;
            background: #fafafa;
            color: #333;
            tab-size: 2;
        }
        
        .editor:focus {
            outline: none;
            background: white;
        }
        
        .preview {
            flex: 1;
            padding: 20px;
            background: white;
            overflow-y: auto;
            border-left: 3px solid #667eea;
            font-size: 16px;
            line-height: 1.7;
        }
        
        /* Markdown样式 */
        .preview h1, .preview h2, .preview h3, .preview h4, .preview h5, .preview h6 {
            margin-top: 2rem;
            margin-bottom: 1rem;
            color: #2c3e50;
            font-weight: 600;
        }
        
        .preview h1 {
            font-size: 2rem;
            border-bottom: 2px solid #eee;
            padding-bottom: 0.5rem;
        }
        
        .preview h2 {
            font-size: 1.5rem;
            border-bottom: 1px solid #eee;
            padding-bottom: 0.3rem;
        }
        
        .preview h3 { font-size: 1.25rem; }
        .preview h4 { font-size: 1.1rem; }
        .preview h5 { font-size: 1rem; }
        .preview h6 { font-size: 0.9rem; color: #666; }
        
        .preview p {
            margin-bottom: 1rem;
            color: #333;
        }
        
        .preview code {
            background: #f4f4f4;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Monaco', 'Menlo', monospace;
            color: #e83e8c;
            font-size: 0.9em;
        }
        
        .preview pre {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            overflow-x: auto;
            margin: 1rem 0;
            border-left: 4px solid #667eea;
        }
        
        .preview pre code {
            background: none;
            padding: 0;
            color: inherit;
            font-size: 0.9em;
        }
        
        .preview blockquote {
            border-left: 4px solid #667eea;
            padding-left: 15px;
            margin: 1rem 0;
            color: #666;
            background: #f9f9f9;
            padding: 10px 15px;
            border-radius: 0 4px 4px 0;
        }
        
        .preview ul, .preview ol {
            margin: 1rem 0;
            padding-left: 2rem;
        }
        
        .preview li {
            margin: 0.5rem 0;
        }
        
        .preview table {
            border-collapse: collapse;
            width: 100%;
            margin: 1rem 0;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        
        .preview th, .preview td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        
        .preview th {
            background: #667eea;
            color: white;
            font-weight: 600;
        }
        
        .preview tr:nth-child(even) {
            background: #f9f9f9;
        }
        
        .preview a {
            color: #667eea;
            text-decoration: none;
            border-bottom: 1px solid transparent;
            transition: border-color 0.3s;
        }
        
        .preview a:hover {
            border-bottom-color: #667eea;
        }
        
        .preview img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin: 1rem 0;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        
        .preview hr {
            border: none;
            height: 2px;
            background: linear-gradient(to right, #667eea, #764ba2);
            margin: 2rem 0;
            border-radius: 1px;
        }
        
        .status {
            padding: 15px 20px;
            background: #e3f2fd;
            color: #1976d2;
            text-align: center;
            font-weight: 500;
            display: none;
        }
        
        .status.show {
            display: block;
            animation: fadeIn 0.3s ease;
        }
        
        .status.error {
            background: #ffebee;
            color: #c62828;
        }
        
        .status.success {
            background: #e8f5e8;
            color: #2e7d32;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
        
        .word-count {
            background: #f8f9fa;
            padding: 8px 15px;
            font-size: 12px;
            color: #666;
            border-top: 1px solid #e0e0e0;
        }
        
        @media (max-width: 768px) {
            .main-content {
                flex-direction: column;
                height: auto;
            }
            
            .editor-panel {
                border-right: none;
                border-bottom: 1px solid #e0e0e0;
            }
            
            .editor, .preview {
                min-height: 300px;
            }
            
            .header h1 {
                font-size: 2rem;
            }
            
            .controls {
                flex-direction: column;
                align-items: stretch;
                gap: 10px;
            }
            
            .file-input-wrapper, .action-btn {
                width: 100%;
                text-align: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📝 Markdown预览工具</h1>
            <p>实时预览您的Markdown文档，支持语法高亮和美观排版</p>
        </div>
        
        <div class="controls">
            <div class="file-input-wrapper">
                <input type="file" class="file-input" id="fileInput" accept=".md,.markdown,.txt">
                <label for="fileInput" class="file-button">
                    📁 选择文件
                </label>
            </div>
            
            <button class="action-btn secondary" id="downloadBtn" onclick="downloadMarkdown()">
                💾 下载文件
            </button>
            
            <button class="action-btn" id="clearBtn" onclick="clearContent()">
                🗑️ 清空内容
            </button>
            
            <button class="action-btn" id="copyBtn" onclick="copyToClipboard()">
                📋 复制内容
            </button>
        </div>
        
        <div class="status" id="status"></div>
        
        <div class="main-content">
            <div class="panel editor-panel">
                <div class="panel-header">
                    <span>📄 Markdown编辑器</span>
                </div>
                <textarea class="editor" id="editor" placeholder="在这里输入Markdown内容...

# 欢迎使用Markdown预览工具

## 功能特色
- **实时预览**: 支持实时渲染Markdown内容
- **语法高亮**: 代码块支持语法高亮
- **文件操作**: 支持文件导入和导出
- **响应式设计**: 完美适配各种设备

## 示例内容

### 代码块
```javascript
function hello() {
    console.log('Hello, Markdown!');
}
```

### 表格
| 功能 | 状态 |
|------|------|
| 预览 | ✅ |
| 编辑 | ✅ |
| 导出 | ✅ |

### 引用
> 这是一个引用示例，支持多行文本和**粗体**、*斜体*等格式。

开始编辑体验吧！"></textarea>
                <div class="word-count" id="wordCount">字符: 0 | 单词: 0</div>
            </div>
            
            <div class="panel">
                <div class="panel-header">
                    <span>👁️ 实时预览</span>
                </div>
                <div class="preview" id="preview"></div>
            </div>
        </div>
    </div>

    <script>
        // 配置marked选项
        marked.setOptions({
            highlight: function(code, lang) {
                if (lang && hljs.getLanguage(lang)) {
                    try {
                        return hljs.highlight(code, { language: lang }).value;
                    } catch (err) {}
                }
                return hljs.highlightAuto(code).value;
            },
            breaks: true,
            gfm: true
        });

        // DOM元素
        const fileInput = document.getElementById('fileInput');
        const editor = document.getElementById('editor');
        const preview = document.getElementById('preview');
        const status = document.getElementById('status');
        const wordCount = document.getElementById('wordCount');
        
        // 文件上传处理
        fileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    editor.value = e.target.result;
                    updatePreview();
                    showStatus(`文件 "${file.name}" 已加载`, 'success');
                };
                reader.readAsText(file);
            }
        });
        
        // 实时预览和字数统计
        editor.addEventListener('input', function() {
            updatePreview();
            updateWordCount();
        });
        
        // 支持Tab键缩进
        editor.addEventListener('keydown', function(e) {
            if (e.key === 'Tab') {
                e.preventDefault();
                const start = this.selectionStart;
                const end = this.selectionEnd;
                
                this.value = this.value.substring(0, start) + '  ' + this.value.substring(end);
                this.selectionStart = this.selectionEnd = start + 2;
            }
        });
        
        // 更新预览
        function updatePreview() {
            const markdownContent = editor.value;
            try {
                preview.innerHTML = marked.parse(markdownContent);
                // 重新应用语法高亮
                preview.querySelectorAll('pre code').forEach((block) => {
                    hljs.highlightElement(block);
                });
            } catch (error) {
                preview.innerHTML = '<p style="color: red;">Markdown解析错误: ' + error.message + '</p>';
            }
        }
        
        // 更新字数统计
        function updateWordCount() {
            const text = editor.value;
            const charCount = text.length;
            const wordCount = text.trim() ? text.trim().split(/\s+/).length : 0;
            
            document.getElementById('wordCount').textContent = `字符: ${charCount} | 单词: ${wordCount}`;
        }
        
        // 下载Markdown文件
        function downloadMarkdown() {
            const content = editor.value;
            if (!content.trim()) {
                showStatus('没有可下载的内容', 'error');
                return;
            }
            
            const blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = `markdown-${new Date().toISOString().split('T')[0]}.md`;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
            
            showStatus('文件下载成功！', 'success');
        }
        
        // 清空内容
        function clearContent() {
            if (editor.value.trim() && !confirm('确定要清空所有内容吗？')) {
                return;
            }
            
            editor.value = '';
            updatePreview();
            updateWordCount();
            showStatus('内容已清空', 'success');
        }
        
        // 复制到剪贴板
        async function copyToClipboard() {
            const content = editor.value;
            if (!content.trim()) {
                showStatus('没有可复制的内容', 'error');
                return;
            }
            
            try {
                await navigator.clipboard.writeText(content);
                showStatus('内容已复制到剪贴板', 'success');
            } catch (err) {
                // fallback for older browsers
                const textArea = document.createElement('textarea');
                textArea.value = content;
                document.body.appendChild(textArea);
                textArea.select();
                document.execCommand('copy');
                document.body.removeChild(textArea);
                showStatus('内容已复制到剪贴板', 'success');
            }
        }
        
        // 显示状态信息
        function showStatus(message, type = '') {
            status.textContent = message;
            status.className = `status show ${type}`;
            setTimeout(() => {
                status.className = 'status';
            }, 3000);
        }
        
        // 初始化
        updatePreview();
        updateWordCount();
        
        // 拖拽文件支持
        const container = document.querySelector('.container');
        
        container.addEventListener('dragover', (e) => {
            e.preventDefault();
            container.style.borderColor = '#667eea';
        });
        
        container.addEventListener('dragleave', (e) => {
            e.preventDefault();
            container.style.borderColor = 'transparent';
        });
        
        container.addEventListener('drop', (e) => {
            e.preventDefault();
            container.style.borderColor = 'transparent';
            
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                const file = files[0];
                if (file.type === 'text/markdown' || file.name.endsWith('.md') || file.name.endsWith('.txt')) {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        editor.value = e.target.result;
                        updatePreview();
                        updateWordCount();
                        showStatus(`文件 "${file.name}" 已加载`, 'success');
                    };
                    reader.readAsText(file);
                } else {
                    showStatus('请拖拽.md或.txt文件', 'error');
                }
            }
        });
    </script>
</body>
</html>